import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 */
@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 传过来的的请求 cookie 中保存了sessionId
        // 通过请求中的 sessionId 就能获得对应的会话
        HttpSession session = req.getSession(false);
        // 而会话中保存了userName
        String userName = (String) session.getAttribute("username");
        // 获取请求之后返回一个html响应
        resp.setContentType("text/html; charset=utf8");
        resp.getWriter().write("<h3>欢迎你！" + userName + "</h3>");
    }
}
